package com.yjx.managedemo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.managedemo.entity.Employees;
import com.yjx.managedemo.mapper.EmployeesDao;
import com.yjx.managedemo.po.EmployeesQuery;
import com.yjx.managedemo.re.PageResult;
import com.yjx.managedemo.service.EmployeesService;
import com.yjx.managedemo.util.MD5Util;
import com.yjx.managedemo.vo.EmployeesCountVO;
import jakarta.annotation.Resource;
import org.apache.catalina.LifecycleState;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EmployeesServiceImpl implements EmployeesService {
    @Resource
    private EmployeesDao employeesDao;

    @Override
    public Employees queryById(Integer employeeId) {
        return employeesDao.queryById(employeeId);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Employees insert(Employees employees) {
        employees.setHireDate(new Date());
        employees.setCreateTime(new Date());
        employees.setIsDeleted(0);
        employeesDao.insert(employees);
        return null;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Employees update(Employees employees) {
        employees.setHireDate(new Date());
        employees.setCreateTime(new Date());
        employeesDao.update(employees);
        return null;
    }

    @Override
    public boolean deleteById(Integer employeeId) {
        return employeesDao.deleteById(employeeId) > 0;
    }


    @Override
    public Employees login(String name, String password) {
        Employees employees = new Employees();
        employees.setName(name);
        password= MD5Util.MD5Encode(password,"utf-8");
        employees.setPassword(password);
        employees = employeesDao.login(employees);
        return employees;
    }

    @Override
    public PageResult queryAllEmployees(EmployeesQuery employeesQuery) {
        PageHelper.startPage(employeesQuery.getPage(), employeesQuery.getLimit());
        List<EmployeesCountVO> employeesList = employeesDao.queryByEmployees(employeesQuery);
        PageInfo pageInfo = new PageInfo(employeesList);

        PageResult pageResult = new PageResult();
        if (employeesList != null) {
            pageResult.setCode(0);
            pageResult.setData(employeesList);
            pageResult.setMsg("查询成功");
            pageResult.setCount((int)pageInfo.getTotal());
        }
        return pageResult;
    }
}
